﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    public class OAuthController : SharpController
    {
        //[Permission("快捷登录", 1,  "002006")]
        public ActionResult Index()
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var data = proxy.Channel.GetOAuthConfigs();

                return View(data);
            }
        }


        public ActionResult Save(List<OAuthConfigDTO> oauthData)
        {
            if (oauthData == null)
            {
                return this.JsonNet(new Result { Err = "oauthData 不能为空" });
            }
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                foreach (var dto in oauthData)
                {
                    var res = proxy.Channel.SaveOAuthConfig(dto);
                    if (res.Err.Length > 0)
                    {
                        return this.JsonNet(res);
                    }
                }
            }
            Task.Run(() =>
            {
                Thread.Sleep(2000);
                HttpRuntime.Close();//重启程序
            });
            return this.JsonNet(new Result());
        }
    }
}